package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;

import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.SelectDeviceGroup;
import com.topboya.linker.model.levelitem.SelectVistorItem;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.adapter.SelectedDepartListAdapter;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.utils.LogUtils;

import java.io.Serializable;
import java.util.List;

public class SelectedDepartListActivity extends BaseActivity {

    private static final String TAG = "SelectedDepartListActivity";
    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private RecyclerView rey_departlist;
    private List<SelectVistorItem> departmentListselected;
    private SelectDeviceGroup docDetailInfo;
    private SelectedDepartListAdapter departListAdapter;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_select_departlist;
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        rey_departlist = findViewById(R.id.rey_departlist);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
    }

    @Override
    protected void initData() {

        tv_toolbar_right.setText("确定");
        departmentListselected = (List<SelectVistorItem>) getIntent().getSerializableExtra("departmentListselected");
        docDetailInfo = (SelectDeviceGroup) getIntent().getSerializableExtra("DocDetailInfo");
        LogUtils.v(TAG + "docDetailInfo:"+docDetailInfo.toString());
        toolbar_title.setText("已选择数量："+departmentListselected.size());
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rey_departlist.setLayoutManager(layoutManager);
        rey_departlist.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        departListAdapter = new SelectedDepartListAdapter(departmentListselected);
        rey_departlist.setAdapter(departListAdapter);
    }

    @Override
    protected void initListener() {
        toolbar.setNavigationOnClickListener(v -> {

            finish();
        });
          departListAdapter.setOnItemChildClickListener((v, position) -> {
//               点击删除按钮进行删除
             departmentListselected.remove(position);
             departListAdapter.notifyDataSetChanged();

          });
          tv_toolbar_right.setOnClickListener(v -> {

              Intent intent = new Intent(SelectedDepartListActivity.this,xCreateTaskActivity.class);
              intent.putExtra("departmentListselected", (Serializable) departmentListselected);
              intent.putExtra("DocDetailInfo", docDetailInfo);
              startActivity(intent);
              finish();

          });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

//   重写返回键，怕用户点删除按钮了，直接返回
    @Override
    public void onBackPressed() {

        back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

        Intent intent = new Intent(SelectedDepartListActivity.this,xCreateTaskActivity.class);
        intent.putExtra("departmentListselected", (Serializable) departmentListselected);
        intent.putExtra("DocDetailInfo", docDetailInfo);
        startActivity(intent);
        finish();


        }

}
